//
//  DMPageControl.m
//  yqApp
//
//  Created by 董强彬 on 2018/10/26.
//  Copyright © 2018 dianmei. All rights reserved.
//

#import "APPageControl.h"
#import "UIImage+Creation.h"
@implementation APPageControl

- (instancetype)init{
    self = [super init];
    if (self) {
        self.userInteractionEnabled = NO;
        _indicatorTintColor = LYRGBColor(242, 242, 242);
        _currentIndicatorTintColor = [LYRGBColor(87, 61, 255) colorWithAlphaComponent:1];
        _indicatorRadius = 4;
    }
    return self;
}

- (void)drawRect:(CGRect)rect{
    if (self.controlStyle == DMPageControlStyleCircle) {
        self.pageIndicatorTintColor = _indicatorTintColor;
        self.currentPageIndicatorTintColor = _currentIndicatorTintColor;
        if (@available(iOS 14.0, *)) {
            if (_indicatorRadius != 4) {
                self.preferredIndicatorImage = [UIImage createImageWithColor: self.pageIndicatorTintColor andSize:CGSizeMake(_indicatorRadius, _indicatorRadius) cornerRadius:_indicatorRadius * 0.5];
            }
        }
        return;
    }
    if (@available(iOS 14.0, *)) {
        self.preferredIndicatorImage = [UIImage createImageWithColor:LYRGBColor(87, 61, 255) andSize:CGSizeMake(12, 4) cornerRadius:2];
        self.backgroundStyle = UIPageControlBackgroundStyleMinimal;
    }
}


- (void)layoutSubviews{
    [super layoutSubviews];
    NSInteger count = [self.subviews count];
    if (self.controlStyle == DMPageControlStyleCircle) {
        if (_indicatorRadius != 4) {
            if (@available(iOS 14.0, *)) {
                
            } else {
                for (NSUInteger subviewIndex = 0; subviewIndex < count; subviewIndex++) {
                    UIImageView* subview = [self.subviews objectAtIndex:subviewIndex];
                    [subview setFrame:CGRectMake(subview.frame.origin.x, subview.frame.origin.y,_indicatorRadius,_indicatorRadius)];
                    subview.layer.cornerRadius = _indicatorRadius * 0.5;
                }
            }
        }
        return;
    }
    if (count) {
        if (@available(iOS 14.0, *)) {
            
        } else {
            for (NSUInteger subviewIndex = 0; subviewIndex < count; subviewIndex++) {
                UIImageView* subview = [self.subviews objectAtIndex:subviewIndex];
                [subview setFrame:CGRectMake(subview.frame.origin.x, subview.frame.origin.y,12,4)];
                subview.layer.cornerRadius = 2;
            }
        }
    }
}

@end
